import 'package:dropdown_button2/dropdown_button2.dart';
import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/components/drawer_title.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/modules/application/special_operation/special_list/job_ticket/common/common_work_all/common_work_all_controller.dart';

/// 特殊作业通用总页面
class CommonWorkAllPage extends StatelessWidget {
  CommonWorkAllPage({super.key});

  final controller = Get.put(CommonWorkAllController());

  ///标题显示
  Widget _appBar(
    int? titleIndex,
  ) {
    if (controller.titleList[titleIndex!].pageTitle == null) {
      return Text(controller.titleList[titleIndex].title,
          style: TextStyle(
            fontSize: 16.sp,
            fontWeight: FontWeight.w500,
          ));
    }
    return Container(
      height: 35.w,
      padding: EdgeInsets.only(left: 16.w, right: 16.w),
      child: Container(
        width: 84.w * controller.titleList[titleIndex].pageTitle!.length,
        decoration: BoxDecoration(
          color: const Color(0x1F5C6DFF),
          border: Border.all(
            color: const Color(0xff5C6DFF),
            width: 1.w,
          ),
          borderRadius: BorderRadius.circular(4.r),
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: controller.titleList[titleIndex].pageTitle!.map((e) {
            final bool isCurrent = controller.titleList[titleIndex].pageTitle?.indexOf(e) == controller.titleList[titleIndex].currentPageTitle;
            return Container(
              width: 81.w,
              height: 28.w,
              decoration: BoxDecoration(
                color: isCurrent ? const Color(0xff5C6DFF) : null,
                borderRadius: BorderRadius.circular(3.r),
              ),
              alignment: Alignment.center,
              child: Text(
                e,
                style: TextStyle(fontSize: 14.sp, color: isCurrent ? Colors.white : const Color(0xff5C6DFF), fontWeight: FontWeight.w500),
              ),
            ).click(() {
              controller.selectTitle(titleIndex, controller.titleList[titleIndex].pageTitle!.indexOf(e));
            });
          }).toList(),
        ),
      ),
    );
  }

  ///右侧按钮
  Widget _action() {
    return Container(
        margin: EdgeInsets.only(right: 16.w),
        child: DropdownButtonHideUnderline(
            child: DropdownButton2<String>(
          items: controller.rightMenuList.map((item) {
            return DropdownMenuItem<String>(
              value: item,
              child: Container(
                alignment: Alignment.centerLeft,
                child: Text(
                  item,
                  overflow: TextOverflow.ellipsis,
                  maxLines: 1,
                  style: TextStyle(
                    fontSize: 18.w,
                    fontWeight: FontWeight.w400,
                    color: const Color(0xffFFFFFF),
                  ),
                ),
              ),
            );
          }).toList(),
          //选择框样式
          dropdownStyleData: DropdownStyleData(
            width: 100.w,
            padding: EdgeInsets.symmetric(vertical: 10.w),
            decoration: BoxDecoration(
              boxShadow: [
                BoxShadow(
                  color: Colors.grey.withOpacity(0.0),
                  spreadRadius: 0,
                  blurRadius: 0,
                ),
              ],
              borderRadius: BorderRadius.circular(4.w),
              color: const Color(0xff434343),
            ),
            offset: const Offset(-60, 0),
          ),
          customButton: Icon(
            Icons.reorder,
            size: 25.w,
            color: const Color(0xff495061),
          ),
          onChanged: (value) {
            controller.jumpToPage(value!);
          },
        )));
  }

  @override
  Widget build(BuildContext context) {
    controller.initRightMenu();
    return GetBuilder(
        init: controller,
        builder: (_) {
          if (controller.titleList.isEmpty) {
            return Scaffold(
              appBar: CustomAppBar(
                titleText: '作业票详情',
              ),
            );
          }
          return Scaffold(
              appBar: CustomAppBar(
                title: _appBar(controller.executeEntity.selectIndex),
                actions: [
                  _action(),
                ],
              ),
              body: Scaffold(
                key: controller.scaffoldKey,
                drawer: Drawer(
                    width: 200.w,
                    child: Container(
                      color: Colors.white,
                      child: Column(
                        children: [
                          DrawerTitle.titleDrawer('收起', AppImages.icon_pack_up).click(() {
                            controller.scaffoldKey.currentState?.closeDrawer();
                          }),
                          Column(
                            children: controller.titleList.map((e) {
                              return DrawerTitle.titleDrawer(e.title, e.icon,
                                      isSelected: controller.executeEntity.selectIndex == controller.titleList.indexOf(e))
                                  .click(() {
                                controller.selectExpandMenu(controller.titleList.indexOf(e));
                                controller.selectLeftMenu(controller.titleList.indexOf(e));
                              });
                            }).toList(),
                          ),
                        ],
                      ),
                    )),
                body: Row(
                  children: [
                    Container(
                      width: 60.w,
                      color: Colors.white,
                      alignment: Alignment.topCenter,
                      child: Column(
                        children: [
                          DrawerTitle.title('展开', AppImages.icon_launch).click(() {
                            controller.scaffoldKey.currentState?.openDrawer();
                          }),
                          Expanded(
                            child: SingleChildScrollView(
                                child: Column(
                              children: controller.titleList.map((e) {
                                return DrawerTitle.title(e.title, e.icon, isSelected: controller.executeEntity.selectIndex == controller.titleList.indexOf(e))
                                    .click(() {
                                  controller.selectLeftMenu(controller.titleList.indexOf(e));
                                });
                              }).toList()
                                ..add(AppGaps.vGap64),
                            )),
                          ),
                        ],
                      ),
                    ),
                    Expanded(
                      child: controller
                          .titleList[controller.executeEntity.selectIndex].pages[controller.titleList[controller.executeEntity.selectIndex].currentPageTitle],
                    ),
                  ],
                ),
                bottomSheet: Visibility(
                    // visible: true,
                    //开票中和进行中的才显示按钮
                    visible: controller.receiptType == 1 || controller.receiptType == 2,
                    child: Container(
                      width: double.infinity,
                      height: 60.w,
                      padding: EdgeInsets.symmetric(horizontal: 16.w),
                      decoration: BoxDecoration(
                        color: Colors.white,
                        boxShadow: [
                          BoxShadow(
                            color: const Color(0xffBCBCC5).withOpacity(0.3), // 阴影颜色
                            offset: const Offset(0, -5), // 阴影的偏移量，向上偏移，即上方阴影
                            blurRadius: 10.0, // 阴影的模糊半径
                            spreadRadius: 2.0, // 阴影的扩散半径
                          ),
                        ],
                      ),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          controller.isShowRevoke()
                              ? Container(
                                  margin: EdgeInsets.only(right: 10.w),
                                  width: 120.w,
                                  height: 40.w,
                                  child: ElevatedButton(
                                      style: ElevatedButton.styleFrom(
                                          backgroundColor: const Color(0xffFFFFFF),
                                          shape: RoundedRectangleBorder(
                                            borderRadius: BorderRadius.circular(22.r),
                                          ),
                                          side: BorderSide(
                                            color: const Color(0xff5C6DFF),
                                            width: 1.w, // 设置边框线的宽度
                                          )),
                                      onPressed: controller.revokeTicketReceipt,
                                      child: Text(
                                        '撤销',
                                        style: TextStyle(
                                          fontSize: 16.sp,
                                          color: const Color(0xff5C6DFF),
                                        ),
                                      )),
                                )
                              : const SizedBox(),
                          controller.isShowNextButton()
                              ? Expanded(
                                  child: Container(
                                      height: 40.w,
                                      alignment: Alignment.center,
                                      decoration: BoxDecoration(
                                          color: const Color(0xff5C6DFF),
                                          borderRadius: BorderRadius.all(
                                            Radius.circular(20.w),
                                          )),
                                      child: Text(
                                        controller.isNextStep() ? '下一步' : '修改完成',
                                        style: TextStyle(
                                          fontSize: 16.sp,
                                          color: const Color(0xffffffff),
                                        ),
                                      )).clickOnce(controller.nextStep),
                                )
                              : Container(height: 40.w)
                        ],
                      ),
                    )),
              ));
        });
  }
}
